package com.znxz.hzqiuxm.ziniublog.user.gateways.controller;

import com.znxz.hzqiuxm.ziniublog.user.domain.dao.UserRepository;
import com.znxz.hzqiuxm.ziniublog.user.domain.entity.UserDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Copyright © 2018年 ziniuxiaozhu. All rights reserved.
 *
 * @Author 临江仙 hzqiuxm@163.com
 * 用户基本控制层
 * @Date 2018/8/31 16:15
 */
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping
    @ResponseBody
    public String getHi(){

        return " Hi, welcome to user ";
    }

    @GetMapping("/getAll")
    @PreAuthorize("hasRole('ADMIN')")
    @ResponseBody
    public List<UserDO> getUsers() {
        return userRepository.findAll();
    }

    @GetMapping("/getUser")
    @PreAuthorize("hasRole('USER')")
    @ResponseBody
    public String getUserNum(@RequestParam  String userName) {
        return userRepository.findByUserName(userName);
    }



}
